package com.roam.service.impl;

import com.roam.entity.User;
import com.roam.entity.UserConcreteInfo;
import com.roam.entity.UserConcreteUsebyUpdate;
import com.roam.entity.Visits;
import com.roam.entity.scene.Scene;
import com.roam.mapper.UserMapper;
import com.roam.mgdb.MongoUtils;
import com.roam.service.UserService;
import com.roam.utils.GaoDeAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MongoUtils mongoUtils;
    @Autowired
    GaoDeAPI gaoDeAPI;

    @Override
    public User login(User user) {

        return userMapper.getByEmailAndPassword(user);
    }

    @Override
    public User getInfoByEmail(String propertyValue){
        return userMapper.getInfoByEmail(propertyValue);
    };
    @Override
    public boolean saveRegisterInfo(String email){
        if(userMapper.saveRegisterInfo(email)==1){
            return true;
        }else{
            return false;
        }
    }
    @Override
    public UserConcreteInfo getConcreteInfo(int id){
        return userMapper.getConcreteInfo(id) ;
    }
/*
该方法用于向用户信息中的Visits新加指定景点
 */
    @Override
    public Boolean markPlaceVisit(int userId, String city, int sceneId) {
        Scene scene = mongoUtils.getScene(city,sceneId);
        String sceneName = scene.getName();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = dateFormat.format(date);
        Visits visits = new Visits(date1,sceneId,sceneName,city);
        try {
            UserConcreteInfo userConcreteInfo = userMapper.getConcreteInfo(userId);
            System.out.println(userConcreteInfo);
            //添加标记景点数据

            List<Visits> TEMPVisitedList = new ArrayList<>();
            List<Visits> preVisits = new ArrayList<>(userConcreteInfo.getVisits());
            if(userConcreteInfo.getVisits().equals(null)){
                preVisits = new ArrayList<>();
            }
            for(Visits  visit:preVisits){
                TEMPVisitedList.add(visit);
            }

            TEMPVisitedList.add(visits);
            userConcreteInfo.setVisits(TEMPVisitedList);
            List<String>templist =new ArrayList<>();
            //添加足迹数据
            for(String footprint:userConcreteInfo.getFootprint()) {
                templist.add(footprint);
            };
            System.out.println(templist);
            if(templist==null){
                templist = new ArrayList<>();
            }
            System.out.println(templist);
            String region = gaoDeAPI.getRegion(sceneName,city);
            System.out.println(region);
            if(region!=null){
                for(String ad:templist){
                    if(ad.equals(region)){
                        templist.remove(region);
                    }
                }
                templist.add(region);
            }
            System.out.println(templist);
            userConcreteInfo.setFootprint(templist);
            UserConcreteUsebyUpdate userConcreteUsebyUpdate = UserConcreteUsebyUpdate.constructByNoId(userId,userConcreteInfo);
            if(userMapper.saveConcreteInfo(userConcreteUsebyUpdate)==1){
                return true;
            }else{
            return false;}
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Boolean updateUserInfo(UserConcreteUsebyUpdate userConcreteUsebyUpdate) {
        System.out.println(userConcreteUsebyUpdate);
        int id = userConcreteUsebyUpdate.getUserID();
        try {
            UserConcreteInfo userConcreteInfo = userMapper.getConcreteInfo(id);
            System.out.println(userConcreteInfo);
            userConcreteUsebyUpdate.setFootprint(userConcreteInfo.getFootprint());
            userConcreteUsebyUpdate.setEmail(userConcreteInfo.getEmail());
            if(userConcreteUsebyUpdate.getTxUrl()==null){
                userConcreteUsebyUpdate.setTxUrl(userConcreteInfo.getTxUrl());
            }
            userConcreteUsebyUpdate.setVisits(userConcreteInfo.getVisits());
            System.out.println(userConcreteUsebyUpdate);
            if(userMapper.saveConcreteInfo(userConcreteUsebyUpdate)==1){
                return true;
            }else{
                return false;}
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public Boolean judgeHavenVisited(int useID, int sceneID,String city) {
        UserConcreteInfo userConcreteInfo = userMapper.getConcreteInfo(useID);
        if (userConcreteInfo != null && userConcreteInfo.getVisits() != null) {
            List<Visits> visits = userConcreteInfo.getVisits();
            for (Visits visit : visits) {
                if (visit.getSceneId() == sceneID && visit.getCity().equals(city)) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public String saveAvatar(int userID, MultipartFile file) {
        UploadTX uploadTX = new UploadTX();
        String url = uploadTX.upload(file,userID);
        if(!url.equals("保存失败")){
            try {
                UserConcreteInfo userConcreteInfo = userMapper.getConcreteInfo(userID);
                userConcreteInfo.setTxUrl(url);
                UserConcreteUsebyUpdate userConcreteUsebyUpdate = UserConcreteUsebyUpdate.constructByNoId(userID,userConcreteInfo);
                userMapper.saveConcreteInfo(userConcreteUsebyUpdate);
                return url;
            }catch (Exception e){
                e.printStackTrace();
                return "保存失败";
            }
        }else{
            return url;
        }
    }


}
